package com.hcss.sys.client.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hcss.common.constant.ConfigConstants;
import com.hcss.common.exception.HcssCommonException;
import com.hcss.paging.service.SchemaPagingByExample;
import com.hcss.qywechat.user.constant.WQyUserConstants;
import com.hcss.qywechat.user.dao.WQyUserMapper;
import com.hcss.qywechat.user.model.WQyUser;
import com.hcss.qywechat.user.model.WQyUserExample;
import com.hcss.qywechat.user.model.vo.WQyUserVO;
import com.hcss.qywechat.user.service.WQyUserService;
import com.hcss.sys.client.constant.HcChrgClientConstants;
import com.hcss.sys.client.dao.HcChrgClientComplexMapper;
import com.hcss.sys.client.dao.HcChrgClientMapper;
import com.hcss.sys.client.model.HcChrgClient;
import com.hcss.sys.client.model.HcChrgClientExample;
import com.hcss.sys.client.model.par.HcChrgClientPar;
import com.hcss.sys.client.model.vo.HcChrgClientVO;
import com.hcss.sys.client.service.HcChrgClientService;
import com.hcss.sys.role.dao.HcSysRoleuserMapper;
import com.hcss.util.BeanCopyUtil;
import com.hcss.util.EmptyUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @描述: 客户表-服务的实现类
 * @作者: fangtao
 * @创建日期: 2020-09-18 14:29:21
 */
@Service("hcChrgClientService")
public class HcChrgClientServiceImpl implements HcChrgClientService {
	
	@Autowired
	private HcChrgClientMapper hcChrgClientMapper;
	@Autowired
	private HcChrgClientComplexMapper hcChrgClientComplexMapper;

	@Autowired
	private WQyUserService wQyUserService;
	@Autowired
	private WQyUserMapper wQyUserMapper;

	@Autowired
	private HcSysRoleuserMapper hcSysRoleuserMapper;
	@Autowired
	ConfigConstants configConstants;
	@Autowired
	private AmqpTemplate amqpTemplate;


	@Override
	public PageInfo< HcChrgClient > queryPagingData( HcChrgClientPar par) throws Exception{
        HcChrgClientExample example = (HcChrgClientExample)SchemaPagingByExample.getBaseExample( HcChrgClientExample.class, par);
        PageHelper.startPage(par.getPage(), par.getPerpage());
        List< HcChrgClient > list =  hcChrgClientMapper.selectByExample(example);
        return new PageInfo<>(list);
	}

	@Override
	public PageInfo< HcChrgClientVO > queryComplexPagingData( HcChrgClientVO model){
		PageHelper.startPage(model.getPage(), model.getPerpage());
		List< HcChrgClientVO > list = hcChrgClientComplexMapper.queryComplexData(model);
		return new PageInfo<>(list);
	}

	@Override
	public List< HcChrgClient > queryAllData( HcChrgClientPar par) throws Exception {
		HcChrgClientExample example = (HcChrgClientExample)SchemaPagingByExample.getBaseExample( HcChrgClientExample.class, par);
		return hcChrgClientMapper.selectByExample(example);
	}

	@Override
	public List< HcChrgClientVO > queryComplexAllData( HcChrgClientVO model){
		return hcChrgClientComplexMapper.queryComplexData(model);
	}

	@Override
	public void insertModel( HcChrgClient model) {
		hcChrgClientMapper.insertSelective(model);
	}

	@Override
	public void updateModel( HcChrgClient model) {
        HcChrgClientExample example = new HcChrgClientExample();
		example.createCriteria().andIdEqualTo(model.getId());
        hcChrgClientMapper.updateByExampleSelective(model, example);
	}

	@Override
	public void deleteByIds(List<String> ids) {

	}

	@Override
	public void deleteByIdsAndSendCargo(List <String> ids) {
		HcChrgClientVO hcChrgClientVO=new HcChrgClientVO();
		hcChrgClientVO.setIds(ids);
		deleteByIds(ids);
	}
	
	@Override
	public HcChrgClient selectByPK( String id) {
		if (EmptyUtil.isHaveEmpty(id)) return null;

        HcChrgClientExample example = new HcChrgClientExample();
        example.createCriteria().andIdEqualTo(id);
		List< HcChrgClient > list = hcChrgClientMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
	}

	@Override
	public HcChrgClientVO selectComplexDataById( String id) {
		if (EmptyUtil.isHaveEmpty(id)) return null;

		HcChrgClientVO hcChrgClientVO = new HcChrgClientVO();
		hcChrgClientVO.withId(id);
		List< HcChrgClientVO > list = hcChrgClientComplexMapper.queryComplexData(hcChrgClientVO);
		return CollectionUtils.isEmpty(list) ? null : list.get(0);
	}

	@Override
	public void uAgentCarrier( HcChrgClientVO model){

	}

	@Override
	public HcChrgClientVO getAgentcarrier( String id) throws Exception{

		return null;
	}

	@Override
	public void addClientVO( HcChrgClientVO model) throws Exception{

	}

	//新增客户_登陆用户
	private void dealWQyUser(WQyUserVO wQyUserVO) throws Exception{

	}

	@Override
	public void updateClientVO ( HcChrgClientVO model) throws Exception{

	}


	@Override
	public void checkClientData( HcChrgClientVO model) throws Exception{
		//校验登录用户
		if(EmptyUtil.isHaveEmpty(model.getId())) {
			WQyUserVO wQyUserVO = BeanCopyUtil.copyConvey(model, WQyUserVO.class);
			if (!wQyUserService.checkCodeUnique(wQyUserVO)) {
				throw new Exception("登陆用户[" + wQyUserVO.getUserid() + "]已存在！");
			}
			if (!model.getPassword().equals(model.getConfirmWord())) {
				throw new Exception(WQyUserConstants.editPasswordCheck.WORD_NOTEQ);
			}
			//校验客户信息
			if (!checkClient(model)) {
				throw new Exception("客户[" + model.getCFullname() + "]已存在！");
			}
		}else{

			if(!checkClient(model)){
				throw new Exception("客户[" + model.getCFullname() + "]已存在！");
			}
		}
	}

	//校验登录用户
	private Boolean checkWQyUser(WQyUserVO model,List<String>userids){
		boolean tempBoolean = true;
		WQyUserExample wQyUserExample = new WQyUserExample();
		wQyUserExample.createCriteria().andUseridEqualTo(model.getUserid()).andUseridNotIn(userids);
		List <WQyUser> wQyUserList = wQyUserMapper.selectByExample(wQyUserExample);
		if (!CollectionUtils.isEmpty(wQyUserList)) {// 未查询到重复code，返回true
			if (EmptyUtil.isHaveEmpty(model.getId())) {
				if (wQyUserList.size() > 0) {
					tempBoolean = false;
				}
			}
		}
		return tempBoolean;
	}

	//校验客户信息
	@Override
	public Boolean checkClient( HcChrgClientVO model){
		boolean tempBoolean = true;
		HcChrgClientExample hcChrgClientExample = new HcChrgClientExample();
		if(EmptyUtil.isHaveEmpty(model.getId())) {
			hcChrgClientExample.createCriteria().andCFullnameEqualTo(model.getCFullname());
		}else{
			hcChrgClientExample.createCriteria().andCFullnameEqualTo(model.getCFullname()).andIdNotEqualTo(model.getId());
		}
		List< HcChrgClient > hcChrgClientList = hcChrgClientMapper.selectByExample(hcChrgClientExample);
		if (!CollectionUtils.isEmpty(hcChrgClientList)) {
			tempBoolean = false;
		}
		return tempBoolean;
	}

	/**
	 * 货代绑定 结算客户 有效性校验
	 * @return
	 */
	@Override
	public  void checkClientValid(String agentcode) throws HcssCommonException {
	}

}
